package mr.ali.video.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import mr.ali.video.web.mapper.CollectionMapper;
import mr.ali.video.web.mapper.CollectionUrlMapper;
import mr.ali.video.web.model.*;
import mr.ali.video.web.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-08-03
 */
@Slf4j
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements ICollectionService {
    @Resource
    private CollectionMapper collectionMapper;
    @Resource
    private ICollectionUrlService collectionUrlService;

    @Resource
    private ICollectionTagsService collectionTagsService;

    @Resource
    private ICollectionActorService collectionActorService;

    @Resource
    private CollectionUrlMapper collectionUrlMapper;

    @Resource
    private ICollectionImgService collectionImgService;

    @Override
    public PageInfo<Collection> getCollectionList(Integer pageNum, Integer pageSize) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        PageHelper.startPage(pageNum, pageSize);
        List<Collection> collections = collectionMapper.selectList(queryWrapper);
        for (Collection collection : collections) {
            Integer row = collectionUrlMapper.CountByUrlVideoId(collection.getId());
            collection.setEpisodes(row);
        }

        return new PageInfo<>(collections);
    }

    @Override
    public Integer updateDisplayed(Integer id, Integer displayed) {
        return collectionMapper.updateDisplayed(id, displayed);
    }

    @Override
    public Integer deleteById(Integer id) {
        collectionTagsService.deleteByVideoId(id);
        collectionActorService.delete(id);
        collectionImgService.deleteByVideoId(id);
        return collectionMapper.deleteById(id);
    }

    @Override
    public Collection selectById(Integer id) {
        List<CollectionUrl> collectionUrls = collectionUrlService.selectByCollectionId(id);
        Collection collection = collectionMapper.selectById(id);
        collection.setCollectionUrls(collectionUrls);
        Integer row = collectionUrlMapper.CountByUrlVideoId(collection.getId());
        collection.setEpisodes(row);
        return collection;
    }

    @Override
    public Integer insert(Collection collection) {
        log.info("collection:{}", collection);
        Integer rows = collectionMapper.insert(collection);
//影视标签
        for (int i = 0; i < collection.getTagsAll().size(); i++) {
            if (collection.getTagsAll().get(i) != null){
                CollectionTags tags = new CollectionTags();
                tags.setVideoId(collection.getId());
                tags.setSortId(collection.getTagsAll().get(i));
                tags.setType(collection.getType());
                tags.setYurt(collection.getYurt());
                tags.setYear(collection.getYear());
                collectionTagsService.insert(tags);
            }
        }
        //影视摘要
        for (int i = 0; i < collection.getImgAll().size(); i++) {
            if(collection.getImgAll().get(i)!= null){
                CollectionImg img = new CollectionImg();
                img.setVideoId(collection.getId());
                img.setImageUrl(collection.getImgAll().get(i));
                img.setGmtCreate(System.currentTimeMillis());
                collectionImgService.insert(img);
            }
        }
//        影视演员
        if (collection.getActorAll().size() > 0) {
            for (int i = 0; i < collection.getActorAll().size(); i++) {
                if (collection.getActorAll().get(i) != null) {
                    CollectionActor actor = new CollectionActor();
                    actor.setVideoId(collection.getId());
                    actor.setActorId(collection.getActorAll().get(i));
                    collectionActorService.insert(actor);
                }
            }
        }
        return rows;
    }
}
